import React, { useRef } from 'react';
import ProForm, { ModalForm, ProFormText, ProFormSelect, ProFormRadio } from '@ant-design/pro-form';
import { FormInstance } from 'antd';
import { OptType } from '@/utils/constants';
import { checkWorkerNoIsExist } from '@/services/human-resources/api';
import { showMsg } from '@/utils/utils';

const OptForm: React.FC<any> = (props) => {
  const { 
    handleModalVisible, 
    handleAdd, 
    actionRef, 
    record = {}, 
    optType = OptType.ADD 
  } = props;

  const formRef = useRef<FormInstance>();

  // 工号唯一性检查
  const checkWorkerNo = async (value: string) => {
    try {
      if (!value) {
        return false;
      }
      const data: {is_exist: string;} = await checkWorkerNoIsExist(value);
      return data.is_exist === "1";
    } catch (error) {
      showMsg(error?.data?.errmsg);
      return false;
    }
  }

  return <ModalForm
      formRef={formRef}
      title="新建/编辑护工信息"
      width="80%"
      visible
      modalProps={{
        maskClosable: false
      }}
      className="worker-modal"
      layout="horizontal"
      onVisibleChange={handleModalVisible}
      onFinish={async (value) => {
        if (optType === OptType.EDIT) {
          value = Object.assign({}, value, { worker_id: record.worker_id });
        }        
        const success = await handleAdd(value as HUMANRESOURCESAPI.WorkerItem);
        if (!success) {
          return;
        }
        handleModalVisible(false);
        actionRef.current?.reload();
      }}
      submitter={{
        searchConfig: {
          resetText: '关闭',
          submitText: '保存'
        }
      }}
      >
      <ProForm.Group>
        <ProFormText
          rules={
            [
              {
                required: true
              },
              {
                validator: async (rule, value) => {
                  if (optType === OptType.ADD) {
                    const isExsit = await checkWorkerNo(value);
                    if (isExsit) {
                      return Promise.reject("工号已存在");
                    } 
                    return Promise.resolve();
                  }
                  return Promise.resolve();
                }
              }
            ]
          }
          label="工号"
          placeholder="请输入工号"
          width="md"
          name="worker_no"
          readonly={!!record.worker_no}
          initialValue={record.worker_no}
        />
      </ProForm.Group>

      <ProForm.Group>
        <ProFormText
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="姓名"
          placeholder="请输入姓名"
          width="md"
          name="worker_name"
          readonly={!!record.worker_name}
          initialValue={record.worker_name}
        />
        <ProFormText
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="身份证号"
          placeholder="请输入身份证号"
          width="md" 
          name="id_no" 
          readonly={!!record.id_no}
          initialValue={record.id_no}
        />
      </ProForm.Group>

      <ProForm.Group>
        <ProFormText
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="联系电话"
          width="md"
          name="mobile"
          initialValue={record.mobile}
        />
      </ProForm.Group>
 

      <ProForm.Group>
        <ProFormSelect
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="星级认证"
          placeholder="请选择"
          width="md"
          name="rank"
          valueEnum={{
            '3': '三星级',
            '4': '四星级',
            '5': '五星级'
          }}
          initialValue={record.rank}
        />
      </ProForm.Group>

      <ProForm.Group>
        <ProFormRadio.Group
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="服务方式"
          width="md"
          name="service_type"
          initialValue={record.service_type || 'I'}
          options={[
            {
              label: '院内',
              value: 'I'
            },
            {
              label: '院外',
              value: 'O'
            },
            {
              label: '全部',
              value: 'A'
            }
          ]}
          />
      </ProForm.Group>

      <ProForm.Group>
        <ProFormRadio.Group
          rules={
            [
              {
                required: true
              }
            ]
          }
          label="在职状态"
          width="md"
          name="job_status"
          initialValue={record.job_status || 'Y'}
          options={[
            {
              label: '在职',
              value: 'Y'
            },
            {
              label: '待岗',
              value: 'W'
            },
            {
              label: '离职',
              value: 'N'
            }
          ]}
        />
      </ProForm.Group>
 
    </ModalForm>
}
export default OptForm;
